package com.example.liunanxin.lnx226ordering.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.example.liunanxin.lnx226ordering.R;
import com.example.liunanxin.lnx226ordering.ShopItemViewHolder;
import com.example.liunanxin.lnx226ordering.beans.AllShopsBean;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ShopAdapter extends RecyclerView.Adapter<ShopItemViewHolder>
{
    private List<AllShopsBean> shopDateList;

    OnItemClickListener onItemClickListener;
    public interface OnItemClickListener{
        void  setItem(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setDate(List<AllShopsBean> dateItemList){
        this.shopDateList=dateItemList;
        notifyDataSetChanged();//閫氱煡鏁版嵁闆嗗彂鐢熷彉鍖�
    }


    @NonNull
    @Override
    public ShopItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = layoutInflater.inflate(R.layout.item_layout,viewGroup,false);
        ShopItemViewHolder viewHolder = new ShopItemViewHolder(itemView);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(@NonNull final ShopItemViewHolder itemViewHolder, final int i) {
        final AllShopsBean shopList = shopDateList.get(i);

        itemViewHolder.textView1.setText(shopList.getShopname());
        itemViewHolder.textView2.setText(shopList.getAddress());
        System.out.println("Shopname:"+shopList.getShopname());
        System.out.println("Address:"+shopList.getAddress());

        Picasso.get()
                .load("http://172.24.10.175:8080/foodService"+shopList.getPic())
                .into(itemViewHolder.imageView);

        itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.setItem(view,i);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(shopDateList!=null)
            return shopDateList.size();
        else
            return 0;
//        return shopDateList.size();//杩斿洖鍒楄〃鏄剧ず鐨勬暟鎹釜鏁�
    }
}

